package it.polimi.traveldream.web.beans.employee;

import it.polimi.traveldream.ejbclient.dtos.HotelDTO;

import it.polimi.traveldream.ejbclient.eaos.BaseProductServiceEAO;
import it.polimi.traveldream.utils.exceptions.NotCoherentException;
import it.polimi.traveldream.web.beans.shared.FacesContextHandler;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;

@ManagedBean(name="addHotel")
@RequestScoped
public class AddHotelBean {
	@EJB
	private BaseProductServiceEAO bService;
	private HotelDTO hotel;
	
	public AddHotelBean() {
	}
	
	@PostConstruct
	public void init() {
		hotel = new HotelDTO();
		hotel.setMaxAvailability(1);
	}
	
	public HotelDTO getHotel() {
		return hotel;
	}

	public void setHotel(HotelDTO hotel) {
		this.hotel = hotel;
	}

	public String addHotel() {
		try {
			bService.addProduct(hotel);
			return "index";
		}  catch (NotCoherentException e) {
			FacesContextHandler.produceFacesMessage(null,FacesContext.getCurrentInstance(),
					FacesMessage.SEVERITY_ERROR, "The composition of the product is not coherent. Please check its fields.");
			return "addHotel";
		}
	}
	
}
